package com.google.code.training_java_basic.basics;

import java.util.Arrays;
import java.util.Scanner;

/**	Class ShortDistance searches two neighboring numbers in an array with the smallest distance to each other. 
*	The index of the first number should be outputted to the screen */

public class ShortDistance {
			
	public static int getIndexValue (int[] numbers){
		
		int minValue;
		int result = 0;

		minValue = Math.abs(numbers[1]-numbers[0]); 
				
		for(int i=1; i < numbers.length;i++){
			if( Math.abs(numbers[i]-numbers[i-1]) < minValue) { 
				minValue =  Math.abs(numbers[i]-numbers[i-1]);
				result = i-1;
			}
		}
	
		return result;
	}

    public static void main(String[] args) {

    	int maxNumber;
		int pos;
		
    	Scanner scanner = new Scanner(System.in);
    	System.out.print("Enter the number of Array values: ");
    	maxNumber = scanner.nextInt();
			
    	int[] digits = new int[maxNumber];
			
    	for(int i=0;i<digits.length;i++){
    		System.out.printf("Enter the value for index %d : ", i);
    		digits[i]=scanner.nextInt();
	   	}
		scanner.close();
		pos = getIndexValue(digits);
		System.out.println("Array: " + Arrays.toString(digits));
		System.out.printf("Index position: %d , (of number %d)", pos, digits[pos]);
				
    }		
			

	}

